package com.lbt.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lbt.commonuntils.JwtUtils;
import com.lbt.educenter.entity.UcenterMember;
import com.lbt.educenter.entity.vo.RegisterVo;
import com.lbt.educenter.mapper.UcenterMemberMapper;
import com.lbt.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lbt.educenter.untils.MD5;
import com.lbt.servicebase.exceptionhandler.GuliException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;


/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-06-06
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {
    @Autowired
    RedisTemplate<String,String> redisTemplate;


    @Override   //登录判断
    public String login(UcenterMember member) {
        String phone = member.getMobile();
        String password = member.getPassword();
        if(StringUtils.isBlank(password)||StringUtils.isBlank(phone)){
           throw new  GuliException(20001,"登录失败，密码或手机号不能为空");
        }
        //手机号是否正确
        QueryWrapper<UcenterMember>wrapper=new QueryWrapper<>();
        wrapper.eq("mobile",phone);
        UcenterMember ucenterMember = baseMapper.selectOne(wrapper);
        if(ucenterMember==null){
            throw new  GuliException(20001,"登录失败，手机号不存在");
        }
        System.out.println("对象属性"+ucenterMember.toString());
        //数据库中的密码是加密过的  所以比较的时候应该加密后比较
        String encrypt = MD5.encrypt(password);
        if(!encrypt.equals(ucenterMember.getPassword())){
            System.out.println("数据库中取到的密码："+ ucenterMember.getPassword());
            throw new  GuliException(20001,"登录失败，密码错误");
        }
        if(ucenterMember.getIsDisabled()){
            throw new  GuliException(20001,"登录失败，账号禁用");
        }
        //登录成功
        String jwtToken = JwtUtils.getJwtToken(ucenterMember.getId(), ucenterMember.getNickname());

      //  redisTemplate.opsForValue().set(phone,jwtToken,5, TimeUnit.MINUTES);
        return jwtToken;
    }

    @Override
    public void register(RegisterVo registerVo) {
        //获取手机验证码
        String code = registerVo.getCode();
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();
        if(StringUtils.isBlank(code)||StringUtils.isBlank(mobile)||
                StringUtils.isBlank(nickname)||StringUtils.isBlank(password)){
            throw new GuliException(20001,"信息错误,注册失败");
        }
        String rediscode = redisTemplate.opsForValue().get(mobile);
        if(!code.equals(rediscode)){
            throw new GuliException(20001,"验证码错误,注册失败");
        }
        //判断手机号是否注册过
        QueryWrapper<UcenterMember>wrapper=new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer integer = baseMapper.selectCount(wrapper);
        if(integer>0){
            throw new GuliException(20001,"该手机号，已经注册");
        }
        UcenterMember ucenterMember=new UcenterMember();
        BeanUtils.copyProperties(registerVo,ucenterMember);
        ucenterMember.setPassword(MD5.encrypt(registerVo.getPassword()));
        baseMapper.insert(ucenterMember);
    }

    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember>wrapper=new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember ucenterMember = baseMapper.selectOne(wrapper);
        return ucenterMember;
    }

    @Override
    public Integer countRegisterDay(String day) {
        return baseMapper.countRegisterDay(day);
    }
}
